layui.define(['jquery','layer','form','element','laydate'], function(exports) {
    var $ = layui.jquery;
    var  layer = layui.layer;
    var form = layui.form();
    var element = layui.element();
    var laydate=layui.laydate;
    var editUsers=function(){};

    editUsers.prototype.init=function(){
        $.post("/srv/rdo.php?", {tpl:"getDct",name:"民族"}, function (d, e) {
           selectShowDataNoH("#nation",JSON.parse(d),"id","name");
           $("#nation").prepend("<option value=''>选择民族</option>");
           $("#nation").val($("#s_nation").val());
           form.render('select');
        });
        $.post("/srv/rdo.php?", {tpl:"getDct",name:"政治面貌"}, function (d, e) {
           selectShowDataNoH("#political_status",JSON.parse(d),"id","name");
           $("#political_status").prepend("<option value=''>选择政治面貌</option>");
           $("#political_status").val($("#s_political_status").val());
           form.render('select');
        });
        $.post("/srv/rdo.php?", {tpl:"getDct",name:"用户身份"}, function (d, e) {
           selectShowDataNoH("#idtype",JSON.parse(d),"id","name");
           $("#idtype").prepend("<option value=''>选择用户身份</option>");
           $("#idtype").val($("#s_idtype").val());
           form.render('select');
        });
        $.post("/srv/rdo.php?", {tpl:"get_dept"}, function (d, e) {
           selectShowDataNoH("#department",JSON.parse(d),"id","deptname");
           $("#department").prepend("<option value=''>选择部门</option>");
           $("#department").val($("#s_department").val());
           form.render('select');
        });
         $.post("/srv/rdo.php?", {tpl:"getDct",name:"职务"}, function (d, e) {
           selectShowDataNoH("#duty",JSON.parse(d),"id","name");
           $("#duty").prepend("<option value=''>选择职务</option>");
           $("#duty").val($("#s_duty").val());
           form.render('select');
        });
         $.post("/srv/rdo.php?", {tpl:"getDct",name:"职称"}, function (d, e) {
           selectShowDataNoH("#dutytitle",JSON.parse(d),"id","name");
           $("#dutytitle").prepend("<option value=''>选择职称</option>");
           $("#dutytitle").val($("#s_dutytitle").val());
           form.render('select');
        });
        $.post("/srv/rdo.php?", {tpl:"get_role"}, function (d, e) {
           selectShowDataNoH("#role",JSON.parse(d),"id","rolename");
           $("#role").prepend("<option value=''>选择角色</option>");
           $("#role").val($("#s_role").val());
           form.render('select');
        });
         $.post("/srv/rdo.php?", {tpl:"getSchool"}, function (d, e) {
           selectShowDataNoH("#school",JSON.parse(d),"id","sch_name");
           $("#school").prepend("<option value=''>选择机构</option>");
           $("#school").val($("#s_school").val());
           form.render('select');
        }); 
        $.post("/srv/rdo.php",{"tpl":"get_dict_list"},function(d){
            var html = "<option value='' childs=''>选择区域</option>"+d;
            $("#areaid").html(html);
            $("#areaid").val($("#s_areaid").val());
            form.render('select');
        }); 
        
    }
    var index = parent.layer.getFrameIndex(window.name);
    $('#btn_save').click(function(){
        if($("#username").val()==""){layer.msg("用户名不能为空");return false;}
        var uname=$("#username").val();
        var id=$("#id").val();
        if(id==""){
            var md5=$("#pmd5").val();
            var emd5=$("#endmd5").val();
            if(md5==""){
                layer.msg("密码不能为空");
                return false;
            }
            if(md5!=emd5){
                layer.msg("两次输入的密码不一样");
                return false;
            }

            $.post("/srv/rdo.php", {tpl:"get_user_byname",uname:uname}, function (d, e) {  
                if(d=="ok"){
                    layer.msg('用户名已存在！');
                    return false;
                }else{
                    $.post("./srv/edit_user.php", $("#form1").serialize(), function (d, e) {
                        if(d=="ok"){
                            layer.msg("保存成功！");
                            setTimeout("parent.layer.close(parent.layer.getFrameIndex(window.name));",1500);
                        }else{
                            layer.msg(d);
                        }
                    });
                }
            });
        }else{
            $.post("/html/user/srv/edit_user.php", $("#form1").serialize(), function (d, e) {
                if(d=="ok"){
                    layer.msg("保存成功！");
                    setTimeout("parent.layer.close(parent.layer.getFrameIndex(window.name));",1500);
                }else{
                     layer.msg(d);
                }
            });
        }
     });
    var fg=$("#s_state").val();
    if(fg==1){
        $(":radio[name='state'][value='1']").prop("checked", "checked");
    }else{
        $(":radio[name='state'][value='0']").prop("checked", "checked");
    }
    var fg=$("#s_sex").val();
    if(fg==1){
        $(":radio[name='sex'][value='1']").prop("checked", "checked");
    }else{
        $(":radio[name='sex'][value='0']").prop("checked", "checked");
    }
    form.render('radio'); 

    var edit_user=new editUsers();
    edit_user.init();
    exports('user_edit',edit_user);
});